﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;

using Entities;
using BLL;

namespace Stationery.UC
{
    public partial class UC_FaqAddUpdate : System.Web.UI.UserControl
    {
        BLL_Faqs bll_Faqs = new BLL_Faqs();
        BLL_Category bll_Category = new BLL_Category();
        Entities.Faqs obj_Faqs = null;
        Entities.User obj_User = null;
        string vFaq_id = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            obj_User = (Entities.User)Session[Utils.SESSION_LOGIN];
            if (obj_User == null)
            {
                Response.Redirect(UtilsURL.home);
            }
            if (Request.Params["status"] != null)
            {
                string strStatus = Request.Params["status"].ToString();
                ViewState["STATUS"] = strStatus;
                
                //Load Infor Faq
                if (strStatus == "edit")
                {
                    vFaq_id = Request.Params["faqid"].ToString();
                    if (vFaq_id != null)
                    {
                        LoadFaqById(Convert.ToInt32(vFaq_id));
                    }
                    else
                    {
                        Response.Redirect(UtilsURL.faq_list);
                    }
                }
            }
        }

        #region Load data        
        private void LoadFaqById(int news_id)
        {
            DataTable dt = new DataTable();
            dt = bll_Faqs.Get_Faqs_ByID(news_id);
            
            if (dt == null)
            {
                return;//ko lam gi
            }
            else
            {
                if (dt.Rows.Count <= 0)
                {
                    Session["datafaq"] = null;
                    Response.Redirect(UtilsURL.faq_list);
                }
                else
                {
                    DataRow dtr = dt.Rows[0];
                    
                    ASPxTextBoxTitle.Text = dtr["Title"].ToString();
                    ASPxHtmlEditorQuestion.Html = dtr["Question"].ToString();
                    ASPxHtmlEditorAnswer.Html = dtr["Answer"].ToString();
                    ASPxTextBoxEmail.Text = dtr["Email"].ToString();                    
                }
            }
        }        
        #endregion

        #region Event Click
        protected void ASPxButtonAccept_Click(object sender, EventArgs e)
        {
            obj_Faqs = new Entities.Faqs();
            Int32 insert_id = 0;
            bool update = false;
            
            if (Request.Params["status"] != null)
            {
                string strStatus = (string)ViewState["STATUS"];
                
                obj_Faqs.TITLE = ASPxTextBoxTitle.Text.Trim();
                obj_Faqs.QUESTION = ASPxHtmlEditorQuestion.Html.ToString();
                obj_Faqs.ANSWER = ASPxHtmlEditorAnswer.Html.ToString();
                obj_Faqs.EMAIL = ASPxTextBoxEmail.Text.Trim();                

                if (strStatus == "edit")
                {
                    if (Request.Params["faqid"] != null)
                    {
                        int faq_id = Convert.ToInt32(Request.Params["faqid"].ToString());
                        obj_Faqs.FAQ_ID = faq_id;
                        
                        update = bll_Faqs.UpdateFaq(obj_Faqs);
                        if (update)
                            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "message", String.Format("alert('Thành công!');location.href='Admin.aspx?id=faq'", UtilsURL.faq_list), true);
                        else
                            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "message", String.Format("alert('KHÔNG Thành công!');location.href='Admin.aspx?id=faq'", UtilsURL.faq_list), true);
                    }
                }
                else if (strStatus == "add")
                {
                    insert_id = bll_Faqs.InsertFaq(obj_Faqs);
                    if (insert_id > 0)
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "message", String.Format("alert('Thành công!');location.href='Admin.aspx?id=faq&status=add'", UtilsURL.faq_add), true);
                    }
                    else
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "message", String.Format("alert('KHÔNG Thành công!');location.href='Admin.aspx?id=faq&status=add'", UtilsURL.faq_add), true);
                }

                Session["datafaq"] = null;
            }
        }

        protected void ASPxButtonCancel_Click(object sender, EventArgs e)
        {
            Session["datafaq"] = null;
            Response.Redirect(UtilsURL.faq_list);
        }
        #endregion
    }
}